package com.jsfcompref.solutech.backing;



import javax.faces.application.FacesMessage;
import javax.faces.component.html.HtmlInputText;
import javax.faces.component.html.HtmlInputTextarea;
import javax.faces.context.FacesContext;

import com.eCommerce.corecontract.dto.CategorieDTO;
import com.eCommerce.server.corebackend.util.BackEndConstants;
import com.eCommercecorecontract.svco.ICategorieSVCO;
import com.eCommercecorecontract.svco.SVCOFinder;
import com.ecommerceWeb.common.MessageFactory;
import com.ecommerceWeb.common.ServiceFinder;



/**
 * Backing bean for register.jsp
 */



public class CreerCategorie
{
  
  private HtmlInputText libelle;
  private HtmlInputTextarea  description;
  private boolean creation;
  private boolean exist;
  private String message=null;
  
 

  public boolean isCreation() {
	return creation;
}

public void setCreation(boolean creation) {
	this.creation = creation;
}

public CreerCategorie()
  {
  }
  
 public String creerCategorie()
 {
	 String status=MessageFactory.status_categorie_failure;
	//modif
	try {
		CategorieDTO categorie = new CategorieDTO();
		categorie.setLibelle(libelle.getValue().toString());
		categorie.setDescription(description.getValue().toString());
		
		SVCOFinder sVCOFinder=(SVCOFinder) ServiceFinder.findBean("SVCOFinder");
		ICategorieSVCO categorieSVCO=(ICategorieSVCO)sVCOFinder.get(BackEndConstants.SVCO_CATEGORIE);
		
		Long id_cat = categorieSVCO.ajouterCategorie(categorie);
		
		setExist(true);
		status=MessageFactory.status_categorie_created;

		String msg ="La categorie ' "+ categorie.getLibelle()+" ' a ete creee avec succes !";
		setMessage(msg);
		init();
		
	} catch (Exception e) {
		FacesContext context = FacesContext.getCurrentInstance();
		context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "Une erreur s'est produite lors de la creation!", null));
		setExist(false);
		status=MessageFactory.status_categorie_failure;
		e.printStackTrace();
	}
		
	
	return status;
  
  }

 public void init()
	{
		 libelle.setValue(null);
		 description.setValue(null);
		 
	}

public HtmlInputText getLibelle() {
	return libelle;
}

public void setLibelle(HtmlInputText libelle) {
	this.libelle = libelle;
}

public HtmlInputTextarea getDescription() {
	return description;
}

public void setDescription(HtmlInputTextarea description) {
	this.description = description;
}

public boolean isExist() {
	return exist;
}

public void setExist(boolean exist) {
	this.exist = exist;
}

public String getMessage() {
	return message;
}

public void setMessage(String message) {
	this.message = message;
}


}
